import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import javax.swing.*;
import java.util.*;
import java.sql.*;

public class servicesPage extends MainFrame
{

  JLabel title;
  int page = 1;
  editServicesFrame editFrame;
    
  
  /* Database variable for page 1 */
  Vector data = new Vector();
  ArrayList <Integer> key = new ArrayList();
  static int picked;
    
  
  /* View Services */
  JComboBox searchFilter;
  JTextField searchBox;
  JButton btnSearch;
  JList results;
  JScrollPane resultsPane;
  JComboBox availability;
  
  /* View Service Details */
  JButton btnEdit;
  JButton btnDelete;
  JButton btnBack;
  
  /* Add Service */
  JTextArea description;
  JTextField price;
  JScrollPane scrollPane;
  JTextField serviceName;
  JCheckBox available;
  JButton btnAddService;
    
   public void Draw(){
       switch(page){
           /* View Services */
           case 1:
                title = new JLabel("View Services");
                title.setFont(new Font("Cambria", Font.PLAIN, 24));
                title.setForeground(Color.black);
                title.setBounds(10,215,300,50);
                MainFrame.panel.add(title);
                
                title = new JLabel("Search");
                title.setFont(new Font("Cambria", Font.PLAIN, 18));
                title.setForeground(Color.black);
                title.setBounds(30,250,300,50);
                MainFrame.panel.add(title);
                
                title = new JLabel("Search Results");
                title.setFont(new Font("Cambria", Font.PLAIN, 18));
                title.setForeground(Color.black);
                title.setBounds(30,320,300,50);
                MainFrame.panel.add(title);
            
                /* Search filter Combobox */
                searchFilter = new JComboBox();
                searchFilter.addItem("All");
                searchFilter.addItem("Name");
                searchFilter.addItem("Availability");
                searchFilter.addItem("Price");
                searchFilter.setBackground(Color.WHITE);
                searchFilter.setBounds(30,290,190,25);
                searchFilter.setSelectedIndex(0);
                searchFilter.setFont(new java.awt.Font("Cambria",java.awt.Font.PLAIN,14));
                searchFilter.addActionListener(new ComboBoxListener());
                MainFrame.panel.add(searchFilter);
                searchFilter.updateUI();
            
                /* Search Box input */
                searchBox = new JTextField();
                searchBox.setBounds(230,290,190,25);
                searchBox.setFont(new java.awt.Font("Cambria",java.awt.Font.PLAIN,14));
                searchBox.setEnabled(false);
                MainFrame.panel.add(searchBox);
                
                /* Gender Combobox */
                availability = new JComboBox();
                availability.addItem("Available");
                availability.addItem("Unavailable");
                availability.setBackground(Color.WHITE);
                availability.setBounds(230,290,190,25);
                availability.setSelectedIndex(0);
                availability.setFont(new java.awt.Font("Cambria",java.awt.Font.PLAIN,14));
                availability.setVisible(false);

                MainFrame.panel.add(availability);
                availability.updateUI();
            
                /* Search Button */
                btnSearch = new JButton(new ImageIcon("searchButton.jpg"));
                btnSearch.setBounds(430,290,70,25);
                btnSearch.addActionListener(new ButtonListener());
                MainFrame.panel.add(btnSearch);
            
                /* Search Results JList */                 
                results = new JList();
                data.removeAllElements();
                results.setFixedCellWidth(400);
                results.setVisibleRowCount(10);
                results.addMouseListener(new ActionJList(results));
                results.setFont(new java.awt.Font("Cambria",java.awt.Font.PLAIN,14));
                resultsPane = new JScrollPane( results );
                resultsPane.setBounds(30,360,470,200);
                resultsPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
                panel.add(resultsPane);
                
                try{
                        database.stmt = database.conn.createStatement();

                        database.rs = database.stmt.executeQuery("SELECT * FROM SERVICE ORDER BY SERVICENAME");
                               
                        key.clear();
                        data.removeAllElements();
                        while( database.rs.next() ){
                           key.add(database.rs.getInt("serviceId"));
                           data.addElement(database.rs.getString("serviceName"));
                        }
                        
                        results.setListData(data);
                           
                } catch( SQLException se ){}
                
                break;
                
           case 2:    
                title = new JLabel("Service Details ");
                title.setFont(new Font("Cambria", Font.PLAIN, 18));
                title.setForeground(Color.black);
                title.setBounds(30,250,200,50);
                panel.add(title);
                
                title = new JLabel("Service Name: ");
                title.setFont(new Font("Cambria", Font.PLAIN, 14));
                title.setForeground(Color.black);
                title.setBounds(30,285,170,50);
                MainFrame.panel.add(title);
                
                title = new JLabel("Price: ");
                title.setFont(new Font("Cambria", Font.PLAIN, 14));
                title.setForeground(Color.black);
                title.setBounds(30,320,170,50);
                MainFrame.panel.add(title);
                
                title = new JLabel("Description: ");
                title.setFont(new Font("Cambria", Font.PLAIN, 14));
                title.setForeground(Color.black);
                title.setBounds(30,355,170,50);
                MainFrame.panel.add(title);
                
                title = new JLabel("Availability: ");
                title.setFont(new Font("Cambria", Font.PLAIN, 14));
                title.setForeground(Color.black);
                title.setBounds(30,465,170,50);
                MainFrame.panel.add(title);
                
                /* What to put in the Service (Information - Name) */
                
                try{
                        // Get a statement from the connection
                        database.stmt = database.conn.createStatement();
                        // Execute the query
                               
                        database.rs = database.stmt.executeQuery("SELECT * FROM SERVICE WHERE SERVICEID = "+ picked );
                               
                        while( database.rs.next() ){

                            title = new JLabel(database.rs.getString("serviceName"));
                            title.setFont(new Font("Cambria", Font.PLAIN, 24));
                            title.setForeground(Color.black);
                            title.setBounds(10,215,300,50);
                            panel.add(title);
                                                   
                            title = new JLabel(database.rs.getString("serviceName"));
                            title.setFont(new Font("Cambria", Font.PLAIN, 14));
                            title.setForeground(Color.black);
                            title.setBounds(140,285,170,50);
                            MainFrame.panel.add(title);
                
                            title = new JLabel("P" + database.rs.getFloat("price"));
                            title.setFont(new Font("Cambria", Font.PLAIN, 14));
                            title.setForeground(Color.black);
                            title.setBounds(140,320,170,50);
                            MainFrame.panel.add(title);
                
                            /* Description of Service Input */
                            description = new JTextArea(database.rs.getString("serviceDescription"));
                            description.setLineWrap(true);
                            description.setWrapStyleWord(true);
                            description.setOpaque(false);
                            description.setFont(new Font("Cambria", Font.PLAIN, 14));
                            description.setEditable(false);
                            scrollPane = new JScrollPane(description);
                            scrollPane.setBounds(140,370,400,100);
                            scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
                            scrollPane.setBorder(null); 
                            scrollPane.setOpaque(false);
                            scrollPane.getViewport().setOpaque(false);
                            MainFrame.panel.add(scrollPane);
                            description.updateUI();
                
                            /* What to put in the Service (Information - Availability) */
                            title = new JLabel(database.rs.getString("availability"));
                            title.setFont(new Font("Cambria", Font.PLAIN, 14));
                            title.setForeground(Color.black);
                            title.setBounds(140,465,170,50);
                            MainFrame.panel.add(title);
                        }
                               
                }
                catch( SQLException se ){}
                
                
                /* Edit Service Button opens up a new frame*/
                btnEdit = new JButton(new ImageIcon("editButton.jpg"));
                btnEdit.setBounds(250,550,92,25);
                btnEdit.addActionListener(new ButtonListener());
                MainFrame.panel.add(btnEdit);
            
                /* Delete Service Button */
                btnDelete = new JButton(new ImageIcon("deleteButton.jpg"));
                btnDelete.setBounds(352,550,92,25);
                btnDelete.addActionListener(new ButtonListener());
                MainFrame.panel.add(btnDelete);
                
                break;
                
                /* Add New Service */
           case 3:
                title = new JLabel("Add New Service");
                title.setFont(new Font("Cambria", Font.PLAIN, 24));
                title.setForeground(Color.black);
                title.setBounds(10,215,300,50);
                MainFrame.panel.add(title);
                
                title = new JLabel("Service Name: ");
                title.setFont(new Font("Cambria", Font.PLAIN, 14));
                title.setForeground(Color.black);
                title.setBounds(30,280,170,50);
                MainFrame.panel.add(title);
                
                title = new JLabel("Price: ");
                title.setFont(new Font("Cambria", Font.PLAIN, 14));
                title.setForeground(Color.black);
                title.setBounds(30,315,170,50);
                MainFrame.panel.add(title);
                
                title = new JLabel("Description: ");
                title.setFont(new Font("Cambria", Font.PLAIN, 14));
                title.setForeground(Color.black);
                title.setBounds(30,350,170,50);
                MainFrame.panel.add(title);
                
                title = new JLabel("Availability: ");
                title.setFont(new Font("Cambria", Font.PLAIN, 14));
                title.setForeground(Color.black);
                title.setBounds(30,460,170,50);
                MainFrame.panel.add(title);
                
                
                /* Service Name input */
                serviceName = new JTextField(10);
                serviceName.setBounds(220,295,190,25);
                serviceName.setFont(new java.awt.Font("Cambria",java.awt.Font.PLAIN,14));
                MainFrame.panel.add(serviceName);
                
                /* Service Name input */
                price = new JTextField(10);
                price.setBounds(220,330,190,25);
                price.setFont(new java.awt.Font("Cambria",java.awt.Font.PLAIN,14));
                MainFrame.panel.add(price);
                
                /* Description of Service Input */
                description = new JTextArea(6, 5);
                description.setEditable(true);
                description.setLineWrap(true);
                description.setFont(new Font("Cambria", Font.PLAIN, 14));
                scrollPane = new JScrollPane(description);
                scrollPane.setBounds(220,365,190,100);
                scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
                MainFrame.panel.add(scrollPane);
                description.updateUI();
                
                /* Availability Checkbox */
                available = new JCheckBox ("Available");
                available.setBounds(220,465,200,40);
                available.setOpaque(false);
                available.setFont(new Font("Cambria", Font.PLAIN, 14));
                MainFrame.panel.add(available);
                
                /* Reserve Button */
                btnAddService = new JButton(new ImageIcon("addServiceButton.jpg"));
                btnAddService.setBounds(125,520,167,25);
                btnAddService.addActionListener(new ButtonListener());
                MainFrame.panel.add(btnAddService);
                
                break;
        }
          title = new JLabel("_____________________________________________________________");
          title.setFont(new Font("Cambria", Font.BOLD, 14));
          title.setForeground(Color.black);
          title.setBounds(0,220,400,50);
          MainFrame.panel.add(title);
   }
   
   public class ButtonListener implements ActionListener{

     public void actionPerformed(ActionEvent e){
        if(e.getSource() == btnBack){
               MainFrame.panel.removeAll();
               MainFrame.servicesPage.page = 1;
               MainFrame.servicesPage.Draw();
               MainFrame.header.Draw();
               MainFrame.panel.add(MainFrame.backgroundImage);
               MainFrame.panel.repaint();     
        }
       else if(e.getSource() == btnSearch){

                      try{
                        database.stmt = database.conn.createStatement();
                        switch(searchFilter.getSelectedIndex()){
                            case 0:
                                database.rs = database.stmt.executeQuery("SELECT * FROM SERVICE ORDER BY SERVICENAME");
                                break;
                           case 1:
                                database.rs = database.stmt.executeQuery("SELECT * FROM SERVICE WHERE SERVICENAME LIKE '%"+searchBox.getText()+"%' ORDER BY SERVICENAME");
                                break;
                           case 2:
                                database.rs = database.stmt.executeQuery("SELECT * FROM SERVICE WHERE AVAILABILITY LIKE '"+availability.getSelectedItem()+"' ORDER BY SERVICENAME");
                                break;
                           case 3:
                                database.rs = database.stmt.executeQuery("SELECT * FROM SERVICE WHERE PRICE LIKE '%"+searchBox.getText()+"%' ORDER BY SERVICENAME");
                                break;
                        }
                        key.clear();
                        data.removeAllElements();
                        while( database.rs.next() ){
                           key.add(database.rs.getInt("serviceId"));
                           data.addElement(database.rs.getString("serviceName"));
                        }
                        results.setListData(data);
                      } catch( SQLException se ){}
                      
       }
       else if(e.getSource() == btnAddService){
           JDialog.setDefaultLookAndFeelDecorated(false);
           int response = JOptionPane.showConfirmDialog(null, "Do you really want to add this service?", 
           "Add New Clinic Service", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
           if (response == JOptionPane.YES_OPTION){   
               
               try{
                    // Get a statement from the connection
                    MainFrame.database.stmt = MainFrame.database.conn.createStatement();
                    // Execute the query
                    String avail;
                    if(available.isSelected() == true)
                        avail = "Available";
                    else
                        avail = "Unavailable";
                        
                    int numUpdated = 
                    MainFrame.database.stmt.executeUpdate("INSERT INTO SERVICE (serviceName,price,serviceDescription,availability)" +
                    " VALUES ('"+serviceName.getText()+"','" + Float.valueOf(price.getText().trim()).floatValue()+"','"+description.getText()
                    +"','"+ avail +"')" ); 
                    
                    
                    MainFrame.database.stmt = MainFrame.database.conn.createStatement();           
                    MainFrame.database.rs = MainFrame.database.stmt.executeQuery("SELECT * FROM SERVICE WHERE SERVICENAME LIKE '"+ serviceName.getText()+"'");
                    
                    while (MainFrame.database.rs.next() )
                        picked = MainFrame.database.rs.getInt("serviceId");
                    
                    JOptionPane.showMessageDialog(null,"Service was successfully added.");
                    
                    MainFrame.panel.removeAll();
                    MainFrame.servicesPage.page = 2;
                    MainFrame.servicesPage.Draw();
                    MainFrame.header.Draw();
                    MainFrame.panel.add(MainFrame.backgroundImage);
                    MainFrame.panel.repaint();
                    
               }catch( SQLException se ){}
           }
       }
       
       else if(e.getSource()==btnEdit){
            editFrame = new editServicesFrame(new JFrame(), "Edit Service Details");
            MainFrame.frame.setEnabled(false);
            editFrame.MainFrame(); 
        }
        
        else if(e.getSource() == btnDelete){
           JDialog.setDefaultLookAndFeelDecorated(false);
           int response = JOptionPane.showConfirmDialog(null, "Are you sure you want to delete this service?", 
           "Remove Service", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
           if (response == JOptionPane.YES_OPTION){   
                 try{
                    database.stmt = database.conn.createStatement();
                    int numUpdated = database.stmt.executeUpdate( "DELETE FROM SERVICE WHERE SERVICEID = " + picked );
                }catch (Exception ex){}
               JOptionPane.showMessageDialog(null,"This service has been successfully deleted.");
               MainFrame.panel.removeAll();
               MainFrame.servicesPage.page = 1;
               MainFrame.servicesPage.Draw();
               MainFrame.header.Draw();
               MainFrame.panel.add(MainFrame.backgroundImage);
               MainFrame.panel.repaint();
           }
        }
     }
   }
   
   public class ComboBoxListener implements ActionListener{
        public void actionPerformed(ActionEvent e) {
            JComboBox cb = (JComboBox) e.getSource();
                if(e.getSource() == searchFilter){
                    int comboboxpic=cb.getSelectedIndex();
                    if(comboboxpic == 0){
                        availability.setVisible(false);
                        searchBox.setVisible(true);
                        searchBox.setEnabled(false);
                    }
                    if(comboboxpic == 1){
                        availability.setVisible(false);
                        searchBox.setVisible(true);
                        searchBox.setEnabled(true);
                    }
                    else if(comboboxpic == 2){
                        availability.setVisible(true);
                        searchBox.setVisible(false);
                    }
                    else if(comboboxpic == 3){
                        availability.setVisible(false);
                        searchBox.setVisible(true);
                        searchBox.setEnabled(true);
                    }
                    searchBox.setText("");
                }
        }
   }
   
    /* Listener for Search Results */
     class ActionJList extends MouseAdapter{
         protected JList list;
    
            public ActionJList(JList l){
                list = l;
            }
    
            public void mouseClicked(MouseEvent e){
                if(e.getSource() == results){
                    if(e.getClickCount() == 2){
                        try{
                            int index = list.locationToIndex(e.getPoint());
                            ListModel dlm = list.getModel();
                            Object item = dlm.getElementAt(index);;
                            list.ensureIndexIsVisible(index);
                        
                        
                            picked = key.get(list.getSelectedIndex());
                            MainFrame.panel.removeAll();
                            MainFrame.servicesPage.page = 2;
                            MainFrame.servicesPage.Draw();
                            MainFrame.header.Draw();
                            MainFrame.panel.add(MainFrame.backgroundImage);
                            MainFrame.panel.repaint();
                        }catch(Exception ex){}
                        
                    }
                }
            }
        }
   
 }
  


